<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { queryLeft2 } from '@/api/business/largeScreen'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null,
      data: [2220, 1682, 2791, 3000, 4090, 3230, 2910],
      xdata: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
    }
  },
  mounted() {
    this.initChart('03')
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(zhlb) {
      this.chart = echarts.init(this.$el, 'macarons')

      queryLeft2({ zhlb: zhlb }).then(res => {
        console.log('queryLeft2:{}', res)
        this.chart.setOption({
          title: {
            text: '每月申请人数',
            left: 20,
            textStyle: {
              color: '#FFFFFF',
              fontSize: 14,
              fontWight: 400
            }
          },
          color: ['#2479d0', '#479A4C', '#F17104'],
          tooltip: {
            trigger: 'item'
          },
          series: [
            {
              name: '每月申请人数',
              type: 'pie',
              radius: '70%',
              center: ['50%', '50%'],
              color: ['#87CEFA', '#00BFFF', '#B0E0E6', '#1E90FF', '#4682B4', '#4169E1'],
              data: res.pie1Array,
              label: {
                normal: {
                  show: false,
                  position: 'inner'
                }
              },
              tooltip: {
                formatter: '{a} <br/>{b} : {c} ({d}%)'
              }
            }
          ]
        })
      })
    }
  }
}
</script>
